/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Parameter;
import org.eclipse.uml2.uml.ParameterDirectionKind;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;


/**
 * This class is used to diplay and modify direction of a parameter.
 */
public class ParameterQualifiersSection extends PropertyViewSection {


	/**
	 * The direction combo.
	 */
	private CCombo directionCombo;
	
	/**
	 * Listener *.
	 */
	private SelectionListener directionComboListener;

	/**
	 * The directionkinds.
	 */
	private final ParameterDirectionKind[] directionkinds= new ParameterDirectionKind[4];

	/**
	 * Creates the controls.
	 * 
	 * @param tabbedPropertySheetPage the tabbed property sheet page
	 * @param parent the parent
	 */
	@Override
	public void createControls(final Composite parent,
			final TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);

		this.directionkinds[0]=ParameterDirectionKind.IN_LITERAL;
		this.directionkinds[1]=ParameterDirectionKind.INOUT_LITERAL;
		this.directionkinds[2]=ParameterDirectionKind.OUT_LITERAL;
		this.directionkinds[3]=ParameterDirectionKind.RETURN_LITERAL;
		final Composite composite = this.getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		this.getWidgetFactory().createCLabel(composite, "Direction:"); //$NON-NLS-1$

		this.directionCombo = this.getWidgetFactory().createCCombo(composite, SWT.NONE);
		this.directionCombo.setEditable(true);
		data = new FormData(100,15);
		data.left = new FormAttachment(0, AbstractPropertySection.STANDARD_LABEL_WIDTH);
		data.top = new FormAttachment(0, 0);
		this.directionCombo.setLayoutData(data);

		this.directionCombo.add(this.directionkinds[0].toString());
		this.directionCombo.add(this.directionkinds[1].toString());
		this.directionCombo.add(this.directionkinds[2].toString());
		this.directionCombo.add(this.directionkinds[3].toString());

		// Add listener on combo
		this.directionCombo.addSelectionListener(
				directionComboListener = new SelectionListener() {
					public void widgetSelected(final SelectionEvent e) {

						((Parameter)ParameterQualifiersSection.this.getElement()).setDirection(ParameterQualifiersSection.this.directionkinds[ParameterQualifiersSection.this.directionCombo.getSelectionIndex()]);
					}

					public void widgetDefaultSelected(final SelectionEvent e){
					}
				});
	}



	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * Refresh the combo from the element.
	 */
	@Override
	public void refresh() {
		if( !this.directionCombo.isDisposed()){
			if (! (this.getElement() instanceof Parameter)) {
				return;
			}

			if (((Parameter)this.getElement()).getDirection()!=null){
				
				// compare with all direction in us array
				for( int i=0; i<this.directionkinds.length;i++){
					if( this.directionkinds[i].equals(((Parameter)this.getElement()).getDirection())){
						this.directionCombo.select(i);
					}
				}
			}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(this.directionCombo!=null && !this.directionCombo.isDisposed())
			this.directionCombo.removeSelectionListener(directionComboListener);
	}
}

